package com.luoran.system.domain.dto;

import cn.hutool.core.collection.ListUtil;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.alibaba.excel.util.ListUtils;
import com.luoran.system.api.domain.SysRole;
import com.luoran.system.service.ISysRoleService;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class RoleDataListener implements ReadListener<SysRole> {

    private static final int BATCH_COUNT = 200000;

    private ISysRoleService iSysRoleService;

    private long nowDateTime = System.currentTimeMillis();



    private static ThreadPoolExecutor ThreadPoolExecutorConfig() {
        // 核心线程数
        int corePoolSize = 15;
        // 最大线程数
        int maximumPoolSize = 30;
        // 线程存活时间
        long keepAliveTime = 90;
        // 时间单位
        TimeUnit unit = TimeUnit.SECONDS;
        // 队列容量
        java.util.concurrent.BlockingQueue<Runnable> workQueue = new java.util.concurrent.ArrayBlockingQueue<>(50);

        ThreadPoolExecutor executor = new ThreadPoolExecutor(
                corePoolSize,
                maximumPoolSize,
                keepAliveTime,
                unit,
                workQueue,
                new ThreadPoolExecutor.AbortPolicy() // 拒绝策略
        );
        return executor;
    }

    public RoleDataListener(ISysRoleService iSysRoleService) {
        this.iSysRoleService = iSysRoleService;
    }

    private List<SysRole> cacheSysRoleList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);


    @Override
    public void invoke(SysRole sysRole, AnalysisContext context) {
        cacheSysRoleList.add(sysRole);
        System.out.println("数组的长度：-------------"+cacheSysRoleList.size());
        if (cacheSysRoleList.size() >= BATCH_COUNT) {

            List<List<SysRole>> lists = ListUtil.split(cacheSysRoleList,5000);

            CountDownLatch downLatch = new CountDownLatch(lists.size());
            for (List<SysRole> roleList : lists) {
                ThreadPoolExecutorConfig().execute( ()->{
                    try {
                        iSysRoleService.loadinRoleMatchAdd(roleList);
                    } catch (Exception a) {
                        System.out.println("线程启动失败");
                    }finally {
                        downLatch.countDown();
                    }
                });
            }
            try {
                downLatch.await();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            cacheSysRoleList.clear();
            lists.clear();
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        System.out.println("数据解析成功");
        //剩余的数据开始批量插入
        if (cacheSysRoleList.size() > 0) {
            List<List<SysRole>> listList = ListUtil.split(cacheSysRoleList, 4000);
            for (List<SysRole> roleList : listList) {
                iSysRoleService.loadinRoleMatchAdd(roleList);
            }
            long endDateTime = System.currentTimeMillis();
            System.out.println("导入方法的时间："+(endDateTime-nowDateTime)+"毫秒");
            System.out.println("数据导入完成");
        }
    }
}
